﻿#region Licence
// <copyright file="Core/ConnectionViewModel.cs" company="Kopigi">
// Copyright © Kopigi 2014
// </copyright>
// ****************************************************************************
// <author>Marc PLESSIS</author>
// <date>11/10/2014</date>
// <project>Nentang.wpf</project>
// <web>http://nentang.codeplex.com</web>
// <license>
// The MIT License (MIT)
// 
// Copyright (c) 2013 Marc Plessis
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// </license>
#endregion
#region using

using System;
using Core.Class;
using Core.Messages;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight.Messaging;
using Interfaces.Enums;

#endregion

namespace Core.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class ConnectionViewModel : ViewModelBaseCore
    {
        #region properties

        /// <summary>
        /// Permet de contrôler la propriété User
        /// </summary>
        private string _user;
        public string User
        {
            get { return _user; }
            set
            {
                _user = value;
                RaisePropertyChanged("User");
            }
        }

        /// <summary>
        /// Permet de contrôler la propriété Password
        /// </summary>
        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }

        
        /// <summary>
        /// Permet de contrôler la propriété MessageErreur
        /// </summary>
        private string _messageErreur;
        public string MessageErreur
        {
            get { return _messageErreur; }
            set
            {
                _messageErreur = value;
                RaisePropertyChanged("MessageErreur");
            }
        }

        public RelayCommand CheckAuthentification { get; set; }
        public RelayCommand CloseApplicationCommand { get; set; }

        #endregion

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public ConnectionViewModel()
        {
            User = Properties.Settings.Default.LastUserConnected;
#if DEBUG
            Password = Properties.Settings.Default.Password;
#endif
            CheckAuthentification = new RelayCommand(CheckingAuthentification);
            CloseApplicationCommand = new RelayCommand(ClosingCommand);
        }

        private void ClosingCommand()
        {
            Messenger.Default.Send<AuthMessage>(new AuthMessage() { IsAuth = false, Closed = true});
        }

        #region RelayCommand functions

        /// <summary>
        /// Permet de vérifier si l'authentification est correcte
        /// </summary>
        private void CheckingAuthentification()
        {
            MessageErreur = string.Empty;

            AccessControlErrorsEnum errorEnum;
            if (IsAuthOk(User, Password, out errorEnum))
            {
                Properties.Settings.Default.LastUserConnected = User;
#if DEBUG
                Properties.Settings.Default.Password = Password;
#else
                Properties.Settings.Default.Password = String.Empty;
#endif
                Properties.Settings.Default.Save();
                Password = string.Empty;
                Messenger.Default.Send<AuthMessage>(new AuthMessage { IsAuth = true});
            }
            else
            {
                switch (errorEnum)
                {
                    case AccessControlErrorsEnum.UtilisateurInconnu:
                        MessageErreur = LocalizedService.LocalizedFromKey("Connection_ErrorUserUnknow");
                        break;
                    case AccessControlErrorsEnum.UtilisateurBloque:
                        MessageErreur = LocalizedService.LocalizedFromKey("Connection_ErrorUserBlock");
                        break;
                    case AccessControlErrorsEnum.MDPIncorrect:
                        MessageErreur = LocalizedService.LocalizedFromKey("Connection_ErrorUsermdpIncorrect");
                        break;
                    default:
                        MessageErreur = LocalizedService.LocalizedFromKey("Connection_ErrorOther");
                        break;
                }
            }
            
        }
        #endregion

        /// <summary>
        /// Permet de valider l'authentification
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="errorEnum"></param>
        /// <returns></returns>
        private bool IsAuthOk(string username, string password, out AccessControlErrorsEnum errorEnum)
        {
            return AccessControlService.IsAuth(username, password, out errorEnum);
        }
    }
}
